package com.ruoyi.web.controller.channel;

import com.ruoyi.basedata.domain.Bank;
import com.ruoyi.basedata.domain.PayKind;
import com.ruoyi.basedata.service.IBankService;
import com.ruoyi.basedata.service.IPayKindService;
import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.domain.MpChannelRSP;
import com.ruoyi.channel.service.IMpChannelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-06
 */
@Controller
@RequestMapping("/channel/channelInfo")
public class MpChannelController extends BaseController
{
    private String prefix = "channel/mpChannel";
	
	@Autowired
	private IMpChannelService mpChannelService;
	@Autowired
	private IBankService bankService;
	@Autowired
	private IPayKindService payKindService;
	
	@RequiresPermissions("channel:channelInfo:view")
	@GetMapping()
	public String mpChannel()
	{
	    return prefix + "/mpChannel";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("channel:channelInfo:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(MpChannel mpChannel)
	{
		startPage();
        List<MpChannel> list = mpChannelService.selectMpChannelList(mpChannel);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("channel:channelInfo:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MpChannel mpChannel)
    {
    	List<MpChannel> list = mpChannelService.selectMpChannelList(mpChannel);
		List<MpChannelRSP> mpChannelRSPList = makeMpChannelRSPList(list);
		ExcelUtil<MpChannelRSP> util = new ExcelUtil<MpChannelRSP>(MpChannelRSP.class);
        return util.exportExcel(mpChannelRSPList, "mpChannel");
    }
	
	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		PayKind payKind = new PayKind();
		mmap.put("pays",payKindService.selectPayKindList(payKind));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("channel:channelInfo:add")
	@Log(title = "渠道信息", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(MpChannel mpChannel, BigDecimal payRate, BigDecimal merchantPayRate, BigDecimal payLimitLeft, BigDecimal payLimitRight, Long[] payIds)
	{

		return mpChannelService.insertMpChannel(mpChannel, payRate, merchantPayRate, payLimitLeft, payLimitRight, payIds);
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		MpChannel mpChannel = mpChannelService.selectMpChannelById(id);
		mmap.put("mpChannel", mpChannel);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("channel:channelInfo:edit")
	@Log(title = "渠道信息", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(MpChannel mpChannel)
	{		
		return mpChannelService.updateMpChannel(mpChannel);
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("channel:channelInfo:remove")
	@Log(title = "渠道信息", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(mpChannelService.deleteMpChannelByIds(ids));
	}


	/**
	 * 渠道状态修改
	 */
	@Log(title = "渠道信息", businessType = BusinessType.DELETE)
	@RequiresPermissions("channel:channelInfo:eidt")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(MpChannel channel) {
		return  toAjax(mpChannelService.changeStatus(channel));
	}


	/**
	 * 内扣状态修改
	 */
	@Log(title = "渠道信息", businessType = BusinessType.DELETE)
	@RequiresPermissions("channel:channelInfo:eidt")
	@PostMapping("/changeInnerStatus")
	@ResponseBody
	public AjaxResult changeInnerStatus(MpChannel channel) {
		return  toAjax(mpChannelService.changeInnerStatus(channel));
	}




	/**
	 * 校验名字
	 */
	@PostMapping("/checkChannelNameUnique")
	@ResponseBody
	public String checkChannelNameUnique(MpChannel mpChannel) {
		return mpChannelService.checkChannelNameUnique(mpChannel);
	}

	/**
	 * 校验类名
	 */
	@PostMapping("/checkClassNameUnique")
	@ResponseBody
	public String checkClassNameUnique(MpChannel mpChannel) {
		return mpChannelService.checkClassNameUnique(mpChannel);
	}

	/**
	 * 渠道支付方法列表
	 */
	@RequiresPermissions("channel:channelInfo:edit")
	@GetMapping("/payMethod/{mpChannelId}")
	public String mpChannelMethod(@PathVariable("mpChannelId") Integer mpChannelId, ModelMap mmap) {
		mmap.put("mpChannelMethod", mpChannelService.selectMpChannelById(mpChannelId));
		return "channel/mpChannelMethod/mpChannelMethod";
	}

	/**
	 * 根据id查找渠道
	 */
	@PostMapping("/findById")
	@ResponseBody
	public TableDataInfo checkChannelNameUnique(Integer id) {
		startPage();
		MpChannel mpChannel = mpChannelService.selectMpChannelById(id);
		List<MpChannel> list = new ArrayList<>();
		list.add(mpChannel);
		return getDataTable(list);
	}


	/**
	 * 渠道-ip白名单列表
	 */
	@RequiresPermissions("channel:channelInfo:iplist")
	@GetMapping("/ipList/{mpChannelId}")
	public String mpChannelIp(@PathVariable("mpChannelId") Integer mpChannelId, ModelMap mmap) {
		mmap.put("mpChannelMethod", mpChannelService.selectMpChannelById(mpChannelId));
		return "channel/mpChannelIp/mpChannelIp";
	}


	/**
	 * 渠道-代付银行列表
	 */
	@RequiresPermissions("channel:channelInfo:edit")
	@GetMapping("/withdrawalBank/{mpChannelId}")
	public String withdrawalBank(@PathVariable("mpChannelId") Integer mpChannelId, ModelMap mmap) {
		Bank bank = new Bank();
		mmap.put("banks", bankService.selectBankList(bank));
		mmap.put("mpChannel", mpChannelService.selectMpChannelById(mpChannelId));
		return "channel/channelWithdrawalBank/channelWithdrawalBank.html";
	}

	/**
	 * 数据封装
	 */
	public List<MpChannelRSP> makeMpChannelRSPList(List<MpChannel> mpChannelList){
		List<MpChannelRSP> mpChannelRSPList = new ArrayList<>();
		for(MpChannel mpChannel : mpChannelList){
			MpChannelRSP mpChannelRSP = new MpChannelRSP();
			mpChannelRSP.setId(mpChannel.getId());
			mpChannelRSP.setName(mpChannel.getName());
			mpChannelRSP.setCode(mpChannel.getCode());
			mpChannelRSP.setStatus(mpChannel.getStatus());
			mpChannelRSP.setContactName(mpChannel.getContactName());
			mpChannelRSP.setContactPhone(mpChannel.getContactPhone());
			mpChannelRSP.setContactEmail(mpChannel.getContactEmail());
			mpChannelRSP.setWithdrawalFee(mpChannel.getWithdrawalFee());
			mpChannelRSP.setMchWithdrawalFee(mpChannel.getMchWithdrawalFee());
			mpChannelRSP.setWithdrawalLimtLeft(mpChannel.getWithdrawalLimtLeft());
			mpChannelRSP.setWithdrawalLimtRight(mpChannel.getWithdrawalLimtRight());
			mpChannelRSP.setClassName(mpChannel.getClassName());
			mpChannelRSP.setReqUrl(mpChannel.getReqUrl());
			mpChannelRSPList.add(mpChannelRSP);
		}
		return mpChannelRSPList;
	}
}
